package com.wholesale.datas;

import java.util.ArrayList;

import com.wholesale.beans.GoodsInfo;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelperAdapter {
	public static final String DB_NAME = "wholeSale.db";// 数据库名
	public static final String TB_NAME = "ShopCar";// 表名
	public static final String TB_NAME_HISTORY = "history";// 表名

	// 字段名
	public static final String ID = "_id";
	public static final String GOODSID = "goodsId";
	public static final String CARTINFOJSON = "cartinfojson";// 商品cartinfo的json数据
	public static final String GOODSOJSON = "goodsjson";// 商品goods的json数据

	Context context;// 上下文对象
	private SQLiteDatabase db;// 数据库对象
	private ShopCarHelper tHelper;// 帮助类

	public DbHelperAdapter(Context context) {
		this.context = context;
	}

	/**
	 * 打开数据库连接
	 * 
	 * @return
	 */
	public DbHelperAdapter open() {
		tHelper = new ShopCarHelper(context);
		db = tHelper.getWritableDatabase();
		return this;
	}

	/**
	 * 关闭连接
	 */
	public void close() {
		tHelper.close();
	}

	/**
	 * 删除
	 * 
	 * @param stu
	 */
	public boolean delete(ShopCar shopCar) {
		int size = db.delete(TB_NAME, ID + "=" + shopCar.getId(), null);
		if (size == 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 全部删除
	 */
	public void deleteAll() {
		db.delete(TB_NAME, null, null);
	}
	
	/**
	 * 删除全部历史记录
	 */
	public void deleteHistoryAll() {
		db.delete(TB_NAME_HISTORY, null, null);
	}

	public boolean update(ShopCar shopCar) {
		ContentValues cv = new ContentValues();
		cv.put(CARTINFOJSON, shopCar.getCartInfo().toJsonString());
		int size = db.update(TB_NAME, cv, ID + "=" + shopCar.getId() , null);
		if (size == 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 插入数据
	 * 
	 * @param stu实体类
	 * @return
	 */
	public long create(ShopCar shopCar) {
		long result = 0;
		ShopCar oldShopCar = getShopCar(shopCar.getGoodsId());
		if (oldShopCar == null) {
			ContentValues cv = new ContentValues();
			cv.put(GOODSID, shopCar.getGoodsId());
			cv.put(CARTINFOJSON, shopCar.getCartInfo().toJsonString());
			result = db.insert(TB_NAME, null, cv);
		} else {
			if(oldShopCar.getCartInfo().getGoodsAttr().equals(shopCar.getCartInfo().getGoodsAttr())){
				oldShopCar.getCartInfo().setGoodsNumber(
						oldShopCar.getCartInfo().getGoodsNumber()+ 1);
				ContentValues cv = new ContentValues();
				cv.put(CARTINFOJSON, oldShopCar.getCartInfo().toJsonString());
				result = db
						.update(TB_NAME, cv, ID + "=" + oldShopCar.getId(), null);
			}else{
				ContentValues cv = new ContentValues();
				cv.put(GOODSID, shopCar.getGoodsId());
				cv.put(CARTINFOJSON, shopCar.getCartInfo().toJsonString());
				result = db.insert(TB_NAME, null, cv);
			}
		}
		return result;
	}

	public ShopCar getShopCar(int goodsId) {
		String[] col = { ID, GOODSID, CARTINFOJSON };
		Cursor cursor = db.query(TB_NAME, col, "GOODSID=" + goodsId, null,
				null, null, null);
		ShopCar shopCar = null;
		if (cursor.moveToNext()) {
			shopCar = new ShopCar();
			shopCar.setId(Integer.valueOf(cursor.getString(0)));
			shopCar.setGoodsId(Integer.valueOf(cursor.getString(1)));
			shopCar.setCartInfo(cursor.getString(2));
		}
		cursor.close();
		return shopCar;
	}

	public int getGoodsCount() {
		ArrayList<ShopCar> shopCars = getAll();
		int count = 0;
		for (ShopCar shopCar : shopCars) {
			count = count + shopCar.getCartInfo().getGoodsNumber();
		}
		return count;
	}

	public void createHistory(GoodsInfo goodsInfo){
		if(!isExist(goodsInfo.getGoodsId())){
			ArrayList<Integer> list = getHistoryCount();
			int size = list.size();
			if(size == 20){
				db.delete(TB_NAME, ID + "=" + list.get(0), null);
			}
			ContentValues cv = new ContentValues();
			cv.put(GOODSID, goodsInfo.getGoodsId());
			cv.put(GOODSOJSON, goodsInfo.toJsonString());
			db.insert(TB_NAME_HISTORY, null, cv);
		}
	}
	
	public boolean isExist(int goodsId){
		String[] col = { ID};
		boolean result = false ;
		Cursor cursor = db.query(TB_NAME_HISTORY, col, "GOODSID=" + goodsId, null,
				null, null, null);
		if(cursor.moveToNext()){
			result = true ;
		}
		cursor.close();
		return result ;
	}
	
	
	/**
	 * 查询全部历史记录
	 * @return
	 */
	public ArrayList<GoodsInfo> getAllHistory() {
		String[] col = { ID, GOODSID, GOODSOJSON };
		Cursor cursor = db.query(TB_NAME_HISTORY, col, null, null, null, null, null);
		ArrayList<GoodsInfo> list = new ArrayList<GoodsInfo>();
		if (cursor.moveToFirst()) {
			do {
				GoodsInfo goodsInfo = new GoodsInfo();
				goodsInfo.jsonAnalytic(cursor.getString(2));
				list.add(goodsInfo);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}
	
	public ArrayList<Integer>  getHistoryCount(){
		String[] col = { ID, GOODSID};
		Cursor cursor = db.query(TB_NAME_HISTORY, col, null, null, null, null, "_id");
		ArrayList<Integer> list = new ArrayList<Integer>();
		if (cursor.moveToFirst()) {
			do {
				list.add(cursor.getInt(0));
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}
	
	/**
	 * 查询全部
	 * 
	 * @return
	 */
	public ArrayList<ShopCar> getAll() {
		String[] col = { ID, GOODSID, CARTINFOJSON };
		Cursor cursor = db.query(TB_NAME, col, null, null, null, null, null);
		ArrayList<ShopCar> list = new ArrayList<ShopCar>();
		if (cursor.moveToFirst()) {
			do {
				ShopCar shopCar = new ShopCar();
				shopCar.setId(Integer.valueOf(cursor.getString(0)));
				shopCar.setGoodsId(Integer.valueOf(cursor.getString(1)));
				shopCar.setCartInfo(cursor.getString(2));
				list.add(shopCar);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}

	/* 数据库帮助类 内部类 */
	public static class ShopCarHelper extends SQLiteOpenHelper {
		public ShopCarHelper(Context context) {
			super(context, DB_NAME, null, 1);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String CREATE_TABLE = "create table " + TB_NAME + "( " + ID
					+ " integer primary key AUTOINCREMENT," + GOODSID
					+ " integer not null," + CARTINFOJSON + " text not null "
					+ ")";
			db.execSQL(CREATE_TABLE);
			String CREATE_TABLE_HISTORY = "create table " + TB_NAME_HISTORY + "( " + ID
					+ " integer primary key AUTOINCREMENT," + GOODSID
					+ " integer not null," + GOODSOJSON + " text not null "
					+ ")";
			db.execSQL(CREATE_TABLE_HISTORY);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("drop table if exists " + TB_NAME);
			db.execSQL("drop table if exists " + TB_NAME_HISTORY);
			onCreate(db);
		}
	}
}
